package com.example.yls.materialdesign_4;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
    private MyFragmentPageAdapter adapter;
    private TextView tv_my;
    private TextView tv_message;
    private TextView tv_find;
    private TextView tv_contact;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //ViewPager
        initFragmentArrayList();
        initTextView();
        adapter=new MyFragmentPageAdapter(getSupportFragmentManager(),
                fragmentArrayList);
        initViewPager();

        //标签布局TabLayout
        initTabLayout();
    }

    private void initTabLayout() {
        tabLayout=findViewById(R.id.tl_tab);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void initViewPager() {
        viewPager=findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);
        //四个TextView被选中或者没有被选中时的颜色
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        tv_message.setBackgroundColor(Color.RED);
                        tv_contact.setBackgroundColor(Color.GREEN);
                        tv_find.setBackgroundColor(Color.GREEN);
                        tv_my.setBackgroundColor(Color.GREEN);
                        break;
                    case 1:
                        tv_message.setBackgroundColor(Color.GREEN);
                        tv_contact.setBackgroundColor(Color.RED);
                        tv_find.setBackgroundColor(Color.GREEN);
                        tv_my.setBackgroundColor(Color.GREEN);
                        break;
                    case 2:
                        tv_message.setBackgroundColor(Color.GREEN);
                        tv_contact.setBackgroundColor(Color.GREEN);
                        tv_find.setBackgroundColor(Color.RED);
                        tv_my.setBackgroundColor(Color.GREEN);
                        break;
                    case 3:
                        tv_message.setBackgroundColor(Color.GREEN);
                        tv_contact.setBackgroundColor(Color.GREEN);
                        tv_find.setBackgroundColor(Color.GREEN);
                        tv_my.setBackgroundColor(Color.RED);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //四个TextView点击事件
    private void initTextView() {
        tv_contact=findViewById(R.id.tv_contact);
        tv_find=findViewById(R.id.tv_find);
        tv_message=findViewById(R.id.tv_message);
        tv_my=findViewById(R.id.tv_my);
        tv_message.setBackgroundColor(Color.RED);

        tv_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1);
            }
        });
        tv_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(0);
            }
        });
        tv_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(2);
            }
        });
        tv_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(3);
            }
        });
    }
    //添加四个布局
    private void initFragmentArrayList() {
        MessageFragment messageFragment=new MessageFragment();
        ContactFragment contactFragment=new ContactFragment();
        FindFragment findFragment=new FindFragment();
        MyFragment myFragment=new MyFragment();
        fragmentArrayList.add(messageFragment);
        fragmentArrayList.add(contactFragment);
        fragmentArrayList.add(findFragment);
        fragmentArrayList.add(myFragment);
    }
}
